const express = require('express')
const router = express.Router()
const users = require('../models/user')
const jwt = require('jsonwebtoken')

// 用户登录，返回 token
router.post('/login', (req, res) => {
  users.findOne({
    username: req.body.username,
    password: req.body.password
  }, (err, data) => {
    if (err) {
      return res.status(500).send('Server Error')
    } else {
      if (data === null) return res.status(200).send({message: '用户不存在'})
      let token = jwt.sign({
        username: data.username,
        password: data.password
      }, "abc", { expiresIn: 60 * 60 * 48 })
      res.send({
        message: '登陆成功',
        status: 200,
        data: {
          token,
          auth: data.auth
        }
      })
    }
  })
})

module.exports = router
